﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Zyg.Utility.Helper;
using Zyg.SSO.Core.Business;

namespace Zyg.SSO.Web.Admin.Account
{
    public partial class AccountDetail : Zyg.SSO.Web.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (!base.Account.HavePermission(Permission.AccountManage))
                {
                    throw new Exception("你无权查看此页面");
                }
                bool parseSucceed;

                Guid id = Request.QueryString["id"].ToGuid(out parseSucceed);

                if (parseSucceed)
                {
                    BindData(id);
                }
                else
                {
                    throw new Exception("对不起，你所访问的页面不存在");
                }
            }
        }

        protected void BindData(Guid id)
        {
            Zyg.SSO.Core.Business.Account account = Zyg.SSO.Core.Business.Account.Load(id);

            if (account == null)
            {
                throw new Exception("对不起，你所访问的页面不存在");
            }

            AccountDateCreated.Text = account.DateCreated.ToString("yyyy年MM月dd日 H点mm分");
            AccountEmail.Text = account.Email;
            AccountName.Text = account.Name;
            AccountRemark.Text = account.Remark;
            AccountRole.Text = account.Role.Name;
            AccountTel.Text = account.Telephone.IsNullOrEmpty() ? "暂未设定" : account.Telephone;

            if (!string.IsNullOrEmpty(account.PictureAddress))
            {
                System.IO.FileInfo fileInfo = new System.IO.FileInfo(Server.MapPath(account.PictureAddress));

                if (fileInfo.Exists)
                {
                    AccountPic.ImageUrl = account.PictureAddress;
                }
                else
                {
                    AccountPic.ImageUrl = "~/Content/Account/no_head.gif";
                }
            }
            else
            {
                AccountPic.ImageUrl = "~/Content/Account/no_head.gif";
            }

            AccountID.Value = account.Id.ToString();
        }

        protected void BtnDelete_Click(object sender, EventArgs e)
        {
            string isAdmin = Request.QueryString["isAdmin"];

            bool parseSucceed;
            Guid id = Request.QueryString["id"].ToGuid(out parseSucceed);
            Zyg.SSO.Core.Business.Account account = Zyg.SSO.Core.Business.Account.Load(id);

            if (account == null)
            {
                WriteBackScript(String.Format("alert('删除成功');window.location.href='{0}List.aspx';", (isAdmin != null) && isAdmin.Equals("true") ? "Admin" : "Account"));
            }

            if (account.Apps.Count > 0)
            {
                WriteBackScript("alert('当前用户已拥有AP服务,不能删除!');");
            }
            else
            {
                account.DeleteOnSave();
                account.Save();

                WriteBackScript(String.Format("alert('删除成功');window.location.href='{0}List.aspx';", (isAdmin != null) && isAdmin.Equals("true") ? "Admin" : "Account"));
            }
        }
    }
}
